package cn.icare.cms.controller.restful;

import cn.icare.cms.UnitService;
import cn.icare.cms.model.ro.AddUnitRO;
import cn.icare.cms.model.ro.EditUnitRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/unit")
@Api(tags = "REST商品计量单位")
public class UnitController extends BaseController {
    @Resource
    private UnitService unitService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/unit
     * method:POST
     *
     * @param ro
     * @return
     */
    @PostMapping()
    @HystrixCommand(fallbackMethod = "createOneFall")

    @ApiOperation(value = "新增单个区域", notes = "t_unit表新增单个区域")
    public ResponseEntity<RespMsgVO> createOne(@RequestBody AddUnitRO ro) {
        Long id = this.unitService.createOne(ro);
        if (id >= 0) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/unit
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个区域", notes = "t_unit表更新单个区域")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditUnitRO ro) {
        if (this.unitService.updateOne(ro)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/unit/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个区域", notes = "t_unit表删除符合主键的单个区域")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Long id) {
        if (this.unitService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/unit?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个区域", notes = "t_unit表删除符合主键的多个区域")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Long> ids) {
        if (this.unitService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/unit/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个区域", notes = "t_unit表返回符合主键的单个区域")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Long id) {
        return super.getRespOk(this.unitService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/unit?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "查询多个区域", notes = "user表返回符合条件的多个区域")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize

                                             ) {

        if(pageSize==0)
        {
            return super.getRespOk(this.unitService.getAllList(name));
        }
        else {
            return super.getRespOk(this.unitService.getList(name, pageNum, pageSize));
        }
    }



    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddUnitRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditUnitRO ro) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Long> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(String name, Integer pageNum, Integer pageSize) {
        return super.getRespFail(this.FALL_STRING);
    }
    //#endregion

}
